<?php
/*
 * ThankPHP
 * @author etongchina <etongchina@gmail.com>
 * @2012.04.05
 * ThankPHP Framework
 */

if(!defined('IN_THANKPHP_SYSTEM')) exit('Access Denied!');

/**
 * class:thankphp
 * @author etongchina
 * @package thankphp
 */
class thankphp {
	/**
	 * 实现单件模式
	 * @static $FLAG
	 * @var bool
	 */
	static $FLAG = false;
	STATIC $CONFIG = array(
		'MCAIP' => array(
			'M'	=>	'index',
			'A'	=>	'index',
			'C'	=>	'index',
			'I'	=>	'index',
			'p'	=>	'index',
		),
		'DIR'	=>	'',
		'PATH'	=>	'core',
		'URL'	=>	'',
		'DATABASE' =>	''
	);
	public $model,$view,$controller,$acl,$urls;
	public $vo = array();
	
	public function __construct(){
		//
	}
	
	/**
	 * 输出错误信息
	 * 
	 * @param string $error
	 */
	private function error($error){
		print $error;exit;
		return;
	}
	
	/**
	 * 获取请求的url
	 * 
	 * @param bool $TF
	 */
	public function getUrl($TF = false) {
		if(!self::$CONFIG['URL']){
			self::$CONFIG['URL'] = $_SERVER['REQUEST_URI'];
		}
		return ($TF)?self::$CONFIG['URL']:$this;
	}
	
	/**
	 * 获取网站根目录
	 * 
	 * @param bool $TF
	 */
	public function getDir($TF = false) {
		if(!self::$CONFIG['DIR']){
			self::$CONFIG['DIR'] = substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT'])+1, -(strlen(self::$CONFIG['PATH'])+1));
		}
		return ($TF)?$CONFIG['URL']:$this;
	}
	
	/**
	 * 获取数据库连接参数
	 * 
	 * @param bool $TF
	 */
	private function getDatabase($TF = false){
		global $DATABASES;
		if(self::$CONFIG['DATABASE'] == false){
			if(isset($DATABASES) && isset($DATABASES['default'])){
				self::$CONFIG['DATABASE'] = $DATABASES['default'];
			}
			//showMsg('没有设置数据库', 'error');
		}
		return ($TF)?self::$CONFIG['DATABASE']:$this;
	}
	
	/**
	 * 实现单件模式
	 * 
	 * @param void
	 * @return self
	 */
	public function getInstance(){
		if(self::$FLAG){
			return self;
		}else{
			self::$FLAG = true;
			return new self();
		}
	}
	
	/**
	 * 设置database
	 * 
	 * @param array $db
	 */
	public function setDatabase(array $db){
		self::$CONFIG['DATABASE']= $db;
		return $this;
	}
	
	/**
	 * 设置url模版，如MACIP
	 * 
	 * @param string $url
	 */
	public function setUrl($url) {
		self::$CONFIG['URL'] = $url;
		return $this;
	}
	
	/**
	 * 设置网站根目录
	 * 
	 * @param string $dir
	 */
	public function setDir($dir){
		self::$CONFIG['DIR'] = $dir;
		return $this;
	}
	
	/**
	 * 初始化dir，url，database等参数
	 * @param void
	 */
	public function init(){
		$this->getDir()->getUrl()->getDatabase();
		$this->initMvc();
	}
	
	/**
	 * 初始化MVC
	 * @param void
	 */
	private function initMvc(){
		return $this;
	}
	
	/**
	 * 开始渲染界面
	 * @param void
	 */
	function dispatcher(){
		global $MODULE_ROOT,$TEMPLATE_DIRS,$URL_EXCEPT,$DEBUG;
		include 'base.class.php';
		include 'urls.class.php';
		include 'model.class.php';
		include 'view.class.php';
		include 'controller.class.php';
		
		$this->init();
		$this->urls = new urls();
		self::$CONFIG['MCAIP'] = $this->urls->mapUrl(); //print_r(self::$CONFIG['MCAIP']);exit;
		if($DEBUG){
			print "M=".self::$CONFIG['MCAIP']['M'];
			print ";C=".self::$CONFIG['MCAIP']['C'];
			print ";A=".self::$CONFIG['MCAIP']['A'];
			print ";I=".self::$CONFIG['MCAIP']['I'];
			print ";P=".self::$CONFIG['MCAIP']['P'];
			print "<br/>";
		}
		if($this->urls->checkDir($MODULE_ROOT.self::$CONFIG['MCAIP']['M'])){
			require_once $MODULE_ROOT.self::$CONFIG['MCAIP']['M'].'/index.php';
		}else{
			
		}
		$db = self::$CONFIG['DATABASE'];
		$dbtype = $db['ENGINE'];
		include THANKPHP_ROOT.'/'.$dbtype.'.class.php';
		
		$class_name = "controller_".self::$CONFIG['MCAIP']['M'];
		$this->controller = new $class_name;
		$this->controller->model = new $dbtype($db);
		$class_name = "view_".self::$CONFIG['MCAIP']['M'];
		$this->controller->view = new $class_name;
		$this->controller->urls = $this->urls;
		
		$func_name = self::$CONFIG['MCAIP']['A'];
		if($this->controller->checkFunc($func_name)){
			$this->controller->$func_name();
		}else{
			self::error("Action not found");
		}
		$this->vo = $this->controller->getVo();
		
		header("Cache-Control: no-cache, must-revalidate");
		//告诉客户端浏览器不使用缓存，兼容HTTP 1.0 协议    
		header("Pragma: no-cache"); 
		//print_r($this->vo);exit;
		include_once $TEMPLATE_DIRS[0]."/".self::$CONFIG['MCAIP']['M']."/".$this->controller->getTpl();
		//dispatcher
		$this->urls->dispatcher();
		$this->controller->dispatcher();
	}
	
}